// pages/ftInfo/ftInfo.js
import { formatDistance } from '../../../utils/util.js';
import Http from '../../../utils/http.js';
const http = new Http();
const app = getApp();
var t;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        isIphone: false,
        review: '', //审核字段
        activeCreateUid: '',
        ipxBottom: 0,
        otherPage: '', //查看他人主页的录入时--02
        ftContent: {},
        // 经验
        experienceArr: [],
        //网络参数
        operatorsValue: [], //运营商名称
        operatorInfoList: [],
        operatorinfo: [],
        operatorCurrent: 0,
        //照片
        description: [],
        sceneImageArr: [],
        photoWallList: [],
        topImg: [],
        // 地图
        mapLongitude: Number,
        mapLatitude: Number,
        mapScale: 12,
        markers: [],
        //操作弹框
        show: false,
        actions: [
            {
                name: '编辑',
                value: 'content-edit',
                color: '#333333',
            },
            {
                name: '删除',
                value: 'content-delete',
                color: '#FF455E',
            },
        ],
        userArr: [],
        userIdArr: [],
        timeArr: [],
        idArr: [],
        praiseflg: '', //本人是否点赞
        praisecnt: 0, //点赞数
        //评论id
        remarkEdit: false,
        remarkId: '', //评论id
        toView: '', //跳转留言id
        commentTop: 101,
        tocommentid: '', //被回复的评论id,回复时传
        placeHoldComment: '欢迎留言讨论', //评论/回复内容
        touid: '',
        toreplyid: '',
        privateflg: '00',
        createuid: '',
    },
    checkInTap(e) {
        wx.navigateTo({
            url: `../../checkIn/checkIn`,
        });
    },
    //添加照片
    addPhotoBtn(e) {
        wx.navigateTo({
            url: `../../../releasePage/pages/addPictures/addPictures?fromWhere=02&title=FT测试点/${this.data.ftContent.networkscene}&id=${this.data.id}&pointType=11`,
        });
    },
    //添加网络参数
    addNetBtn(e) {
        wx.navigateTo({
            url: `../../../releasePage/pages/operatorInfo/operatorInfo?id=${this.data.id}`,
        });
    },
    editNet(e) {
        wx.navigateTo({
            url: `../../../releasePage/pages/operatorInfo/operatorInfo?netId=${e.currentTarget.dataset.netid}&type=01&id=${this.data.id}&operid=${e.currentTarget.dataset.operid}`,
        });
    },
    // 添加经验
    experienceTap(e) {
        wx.navigateTo({
            url: `../experienceDotsList/experienceDotsList?id=${this.data.id}&typeName=${this.data.ftContent.networkscene}&dotType=FT测试点`,
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _that = this;
        wx.getSystemInfo({
            success: function (res) {
                let isIpx;
                //model中包含着设备信息
                var model = res.model;
                if (model.search('iPhone') != -1) {
                    isIpx = true;
                } else {
                    isIpx = false;
                }
                _that.setData({
                    isIphone: isIpx,
                });
            },
        });
        if (app.globalData.isIpx) {
            _that.setData({
                ipxBottom: 34,
            });
        }
        _that.setData({
            id: options.id,
            otherPage: options.otherPage || '',
            review: options.review || '',
        });
        _that.getFtContent();
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    onReady() {
        this.footComment = this.selectComponent('#foot-comment');
    },
    refresh() {
        this.setData({
            ftContent: {},
            experienceArr: [],
            operatorInfoList: [],
            operatorsValue: [],
            operatorinfo: [],
            description: [],
            sceneImageArr: [],
            photoWallList: [],
            topImg: [],
            userArr: [],
            userIdArr: [],
            timeArr: [],
            idArr: [],
            markers: [],
        });
        this.getFtContent();
    },

    //导航
    navigateBtn(e) {
        let _that = this;
        wx.openLocation({
            //​使用微信内置地图查看位置。
            latitude: _that.data.mapLatitude,
            longitude: _that.data.mapLongitude, //要去的经度-地址
            name: _that.data.ftContent.networkscene,
            address: _that.data.ftContent.address,
        });
    },
    //显示弹出层
    editBtn(e) {
        console.log(e.detail);
        this.setData({
            activeCreateUid: e.target.dataset.uid,
            show: true,
            actions: [
                {
                    name: '编辑',
                    value: 'content-edit',
                    color: '#333333',
                },
                {
                    name: '删除',
                    value: 'content-delete',
                    color: '#FF455E',
                },
            ],
        });
    },
    //取消
    onCancel(e) {
        this.setData({
            show: false,
        });
    },
    onDelect(e) {
        let _that = this;
        console.log(e.detail.name);
        if (e.detail.value == 'content-edit') {
            wx.navigateTo({
                url: `../../../releasePage/pages/homePageInfo/homePageInfo?current=0&id=${_that.data.id}`,
            });
            _that.setData({
                show: false,
            });
        } else {
            _that.setData({
                show: false,
            });
            wx.showModal({
                title: '提示',
                content: '确认要删除该项吗？',
                success: function (res) {
                    if (res.confirm) {
                        if (e.detail.value == 'content-delete') {
                            //文章内容删除
                            wx.request({
                                url:
                                    app.globalData.rootPath +
                                    `cjMap/deleteFtSpecialLocation`,
                                method: 'GET',
                                header: {
                                    'content-type':
                                        'application/x-www-form-urlencoded',
                                },
                                data: {
                                    authId: app.globalData.authId,
                                    id: _that.data.id,
                                    lat: app.globalData.locationLat,
                                    lng: app.globalData.locationLng,
                                },
                                success: function (res) {
                                    wx.showToast({
                                        title: '删除成功',
                                        icon: 'success',
                                        duration: 2000,
                                    });
                                    let pages = getCurrentPages(); //当前页面栈
                                    let prevPage = pages[pages.length - 2]; //上一页面
                                    prevPage.refresh(['ftPoint']);
                                    setTimeout(function () {
                                        wx.navigateBack({
                                            delta: 1, // 返回上一级页面。
                                        });
                                    }, 2000);
                                },
                            });
                        } else if (e.detail.value == 'remark-delete') {
                            //评论删除
                            http.getRequest(
                                `cjMap/deleteComment?authId=${app.globalData.authId}&id=${_that.data.remarkId}`
                            ).then((res) => {
                                if (res.status == 0) {
                                    wx.showToast({
                                        title: '删除成功',
                                        icon: 'none',
                                        duration: 1500,
                                    });
                                    _that.refreshRemark();
                                }
                            });
                        }
                    } else {
                        _that.setData({
                            show: true,
                        });
                        console.log('点击取消回调');
                    }
                },
            });
        }
    },
    choiceOperatorBtn(e) {
        this.setData({
            operatorCurrent: e.target.dataset.index,
        });
    },
    //获取ft内容
    getFtContent() {
        let _that = this;
        http.getRequest(
            `cjMap/getFtSpecialLocationById?authId=${app.globalData.authId}&id=${_that.data.id}`
        ).then((res) => {
            if (res.status == 0) {
                wx.setNavigationBarTitle({
                    title: res.ftSpecialLocation.city + '/FT测试点',
                });
                let operatorValue = '';
                if (res.operatorInfoList.length) {
                    for (let i = 0; i < res.operatorInfoList.length; i++) {
                        // 兼容录的错误数据
                        if (res.operatorInfoList[i].operatorinfo) {
                            res.operatorInfoList[i].operatorinfo = JSON.parse(
                                res.operatorInfoList[i].operatorinfo
                            );
                        }
                        if (
                            res.operatorInfoList[i].coupdatetime ==
                            res.operatorInfoList[i].cocreatetime
                        ) {
                            res.operatorInfoList[i].coupdatetime = '';
                        } else {
                            res.operatorInfoList[i].coupdatetime =
                                res.operatorInfoList[i].coupdatetime.slice(
                                    0,
                                    4
                                ) +
                                '-' +
                                res.operatorInfoList[i].coupdatetime.slice(
                                    5,
                                    7
                                ) +
                                '-' +
                                res.operatorInfoList[i].coupdatetime.slice(
                                    8,
                                    10
                                );
                        }
                        res.operatorInfoList[i].cocreatetime =
                            res.operatorInfoList[i].cocreatetime.slice(0, 4) +
                            '-' +
                            res.operatorInfoList[i].cocreatetime.slice(5, 7) +
                            '-' +
                            res.operatorInfoList[i].cocreatetime.slice(8, 10);
                        operatorValue =
                            operatorValue +
                            '/' +
                            res.operatorInfoList[i].operator;
                        _that.data.operatorsValue.push(
                            res.operatorInfoList[i].operatorpicurl
                        );
                        _that.setData({
                            operatorsValue: _that.data.operatorsValue,
                        });
                    }
                }
                if (!!operatorValue) {
                    operatorValue =
                        operatorValue.substr(1) +
                        '：' +
                        res.ftSpecialLocation.networkscene;
                } else {
                    operatorValue = res.ftSpecialLocation.networkscene;
                }
                let markersItem = {
                    iconPath: '../../../images/homepage/ft_icon.png',
                    id: 0,
                    width: 60,
                    height: 60,
                    latitude: res.ftSpecialLocation.lat,
                    longitude: res.ftSpecialLocation.lng,
                    callout: {
                        content: ' ' + operatorValue + ' ',
                        fontSize: 13,
                        color: '#ffffff',
                        bgColor: '#000000AA',
                        padding: 6,
                        borderRadius: 30,
                        boxShadow: '4rpx 8rpx 16rpx 0 rgba(0)',
                        display: 'ALWAYS',
                        anchorY: 10,
                    },
                };
                _that.data.markers.push(markersItem);
                if (res.photoWallList.length) {
                    for (let i = 0; i < res.photoWallList.length; i++) {
                        _that.data.description.push(
                            res.photoWallList[i].picdescription
                        );
                        _that.data.sceneImageArr.push(
                            res.photoWallList[i].picurl
                        );
                        _that.data.userArr.push(
                            res.photoWallList[i].createuname
                        );
                        let checkinTime = app.timeago(
                            res.photoWallList[i].checkintime ||
                                res.photoWallList[i].createtime
                        );
                        _that.data.timeArr.push(checkinTime);
                        _that.data.idArr.push(res.photoWallList[i].id);
                        _that.data.userIdArr.push(
                            res.photoWallList[i].createuid
                        );
                    }
                }
                if (
                    res.ftSpecialLocation.updatetime ==
                    res.ftSpecialLocation.createtime
                ) {
                    res.ftSpecialLocation.updatetime = '';
                } else {
                    res.ftSpecialLocation.updatetime =
                        res.ftSpecialLocation.updatetime.slice(0, 4) +
                        '-' +
                        res.ftSpecialLocation.updatetime.slice(5, 7) +
                        '-' +
                        res.ftSpecialLocation.updatetime.slice(8, 10);
                }
                res.ftSpecialLocation.createtime =
                    res.ftSpecialLocation.createtime.slice(0, 4) +
                    '-' +
                    res.ftSpecialLocation.createtime.slice(5, 7) +
                    '-' +
                    res.ftSpecialLocation.createtime.slice(8, 10);
                res.ftSpecialLocation.distance = formatDistance(
                    res.ftSpecialLocation.distance
                );
                _that.setData({
                    activeCreateUid: res.ftSpecialLocation.createuid,
                    createuid: res.ftSpecialLocation.createuid,
                    ftContent: res.ftSpecialLocation,
                    operatorInfoList: res.operatorInfoList,
                    mapLongitude: res.ftSpecialLocation.lng,
                    mapLatitude: res.ftSpecialLocation.lat + 0.0008, //是标记点视角下移
                    markers: _that.data.markers,
                    description: _that.data.description,
                    sceneImageArr: _that.data.sceneImageArr,
                    experienceArr: res.experienceList,
                    userArr: _that.data.userArr,
                    timeArr: _that.data.timeArr,
                    idArr: _that.data.idArr,
                    userIdArr: _that.data.userIdArr,
                    praiseflg: res.ftSpecialLocation.praiseflg,
                    praisecnt: res.ftSpecialLocation.praisecnt,
                });
            }
        });
    },

    // 图片预览
    previewImage(e) {
        const current = e.target.dataset.src;
        const currentArr = e.target.dataset.srcarr;
        wx.previewImage({
            current,
            urls: currentArr,
        });
    },
    // 复制
    copyText: function (e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.link,
            success: function (res) {
                wx.getClipboardData({
                    success: function (res) {},
                });
            },
        });
    },
    //打电话
    callPhone(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.phone, //仅为示例，并非真实的电话号码
        });
    },

    //评论
    jumpOrRemark(e) {
        if (this.data.commentTop > 100) {
            this.setData({
                toView: 'comment',
                commentTop: 0,
            });
        }
        if (e.detail.setplaceHold == '00') {
            this.footComment.articalRemark();
            this.onResetInputContent();
        }
    },

    // 重置输入框内容
    onResetInputContent() {
        this.setData({
            placeHoldComment: '欢迎留言讨论',
            tocommentid: '',
            touid: this.data.createuid,
            privateflg: '00',
        });
    },

    scrollTopFun(e) {},

    remarkInfo(e) {
        let _that = this;
        _that.footComment.remarkFocus('01');
        let name = e.detail.battleTag;
        _that.setData({
            tocommentid: e.detail.contentid || e.detail.tocommentid, //被回复的评论id,回复时传
            placeHoldComment: '@' + name, //评论/回复内容
            touid: e.detail.touid,
            toreplyid: e.detail.toreplyid,
            privateflg: e.detail.privateflg,
        });
    },
    remarkEditChange(e) {
        this.setData({
            remarkEdit: e.detail.remarkEdit, //被回复的评论id,回复时传
        });
    },
    //删除评论
    deleteRemark(e) {
        this.setData({
            show: true,
            actions: [
                {
                    name: '删除',
                    value: 'remark-delete',
                    color: '#FF455E',
                },
            ],
            remarkId: e.detail.id,
            activeCreateUid: e.detail.createuid,
        });
    },

    //操作评论后刷新
    refreshRemark() {
        let _that = this;
        http.getRequest(
            `cjMap/getFtSpecialLocationById?authId=${app.globalData.authId}&id=${_that.data.id}`
        ).then((res) => {
            if (res.status == 0) {
                if (
                    res.ftSpecialLocation.updatetime ==
                    res.ftSpecialLocation.createtime
                ) {
                    res.ftSpecialLocation.updatetime = '';
                } else {
                    res.ftSpecialLocation.updatetime =
                        res.ftSpecialLocation.updatetime.slice(0, 4) +
                        res.ftSpecialLocation.updatetime.slice(5, 7) +
                        res.ftSpecialLocation.updatetime.slice(8, 10);
                }
                res.ftSpecialLocation.createtime =
                    res.ftSpecialLocation.createtime.slice(0, 4) +
                    res.ftSpecialLocation.createtime.slice(5, 7) +
                    res.ftSpecialLocation.createtime.slice(8, 10);
                res.ftSpecialLocation.distance = formatDistance(
                    res.ftSpecialLocation.distance
                );
                _that.setData(
                    {
                        ftContent: res.ftSpecialLocation,
                        tocommentid: '', //被回复的评论id,回复时传
                        placeHoldComment: '欢迎留言讨论', //评论/回复内容
                    },
                    () => {
                        let cont = {
                            id: _that.data.id,
                            commentcount: res.ftSpecialLocation.commentcount,
                            type: 'ftPoint',
                        };
                        let pages = getCurrentPages(); //当前页面栈
                        let prevPage = pages[pages.length - 2]; //上一页面
                        if (typeof prevPage.refreshCommentNum == 'function') {
                            prevPage.refreshCommentNum(cont);
                        }
                    }
                );
            }
        });
    },
});
